//
//  WHLoginView.m
//  WHPersonalData
//
//  Created by shengmeng chen on 2021/7/28.
//  Copyright © 2021 mac. All rights reserved.
//

#import "WHLoginView.h"
#import "SVProgressHUD.h"
#import <sys/utsname.h>
#import "CommonTools.h"
#import "SDAutoLayout.h"

#define fontSize 16
#define MarginSize 30

static int count = 0;
@interface WHLoginView ()<UITextFieldDelegate>

@property(nonatomic,retain)dispatch_source_t _timer;

@property (nonatomic,weak)UILabel *mTitleLaber;

@property (nonatomic,weak)UILabel *mTitle1Laber;

/**    手机号  */
@property (nonatomic,weak)UITextField*iphoneTF;
@property (nonatomic,weak)UILabel *mNumLaber;
@property (nonatomic,weak)UIView *mLineView;

/**    验证码  */
@property (nonatomic,weak)UIButton *mVerifyButton;
@property (nonatomic,weak)UITextField *mVerifyTF;
@property (nonatomic,weak)UIView *mLine1View;

/**    密码  */
@property (nonatomic,weak)UITextField *mPassWordTF;

/**    登录方式改变  */
@property (nonatomic,weak)UIButton *mLoginChangeButton;

/**    注册  */
@property (nonatomic,weak)UIButton *loginButton;

/**    去登录  */
@property (nonatomic,weak)UIView *mloginView;
@property (nonatomic,weak)UILabel *mGoTitleLaber;
@property (nonatomic,weak)UIButton *mGoLoginButton;

@end

@implementation WHLoginView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpAllChilds];
        
        self.backgroundColor= [UIColor clearColor];
    }
    return self;
}

-(void)setUpAllChilds
{
    
    UILabel *titleLaber = [[UILabel alloc]init];
    titleLaber.text = @"你好，";
    titleLaber.font = [UIFont systemFontOfSize:30];
    titleLaber.textAlignment = NSTextAlignmentLeft;
    [self addSubview:titleLaber];
    _mTitleLaber = titleLaber;
    
    UILabel *title1Laber = [[UILabel alloc]init];
    title1Laber.text = @"欢迎登录钓友之家";
    title1Laber.font = [UIFont systemFontOfSize:30];
    [self addSubview:title1Laber];
    _mTitle1Laber = title1Laber;
    
    /**    手机号码  */
    UILabel *numLaber = [[UILabel alloc]init];
    numLaber.text = @"+86";
    numLaber.font = [UIFont systemFontOfSize:fontSize];
    numLaber.textColor = RGBColor(106, 168, 255);
    [self addSubview:numLaber];
    _mNumLaber = numLaber;
    
    UITextField*iphoneTF =[[UITextField alloc]init];
    iphoneTF.font = [UIFont systemFontOfSize:fontSize];
    iphoneTF.placeholder  = @" 请输入手机号";
    iphoneTF.keyboardType = UIKeyboardTypeNumberPad;
    iphoneTF.borderStyle = UITextBorderStyleNone;
    _iphoneTF = iphoneTF;
    [self addSubview:iphoneTF];
    
    /**   下划线  */
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineView];
    _mLineView = lineView;
    
    /**    验证码  */
    UIButton *verifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [verifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [verifyButton setTitle:@"获取验证码" forState:UIControlStateSelected];
    [verifyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [verifyButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    verifyButton.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    verifyButton.tag = 100;
    [verifyButton addTarget:self action:@selector(buttonChick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:verifyButton];
    _mVerifyButton = verifyButton;
    
    UITextField*verifyTF =[[UITextField alloc]init];
    verifyTF.font = [UIFont systemFontOfSize:fontSize];
    verifyTF.placeholder =@"请输入验证码";
    verifyTF.keyboardType = UIKeyboardTypeNumberPad;
    verifyTF.borderStyle = UITextBorderStyleNone;
    verifyTF.returnKeyType = UIReturnKeyDone; // 返回键类型
    verifyTF.delegate = self;
    verifyTF.tag = 1;
    [self addSubview:verifyTF];
    _mVerifyTF = verifyTF;
    
    /**   密码  */
    UITextField*passWordTF =[[UITextField alloc]init];
    passWordTF.font = [UIFont systemFontOfSize:fontSize];
    passWordTF.placeholder =@"请输入密码";
    passWordTF.secureTextEntry = YES; // 关键属性：设置为密码框
    passWordTF.borderStyle = UITextBorderStyleNone;
    passWordTF.returnKeyType = UIReturnKeyDone; // 返回键类型
    passWordTF.delegate = self;
    passWordTF.tag = 2;
    passWordTF.hidden = YES;
    [self addSubview:passWordTF];
    _mPassWordTF = passWordTF;
    
    // 添加显示/隐藏密码按钮
    UIButton *toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    toggleButton.frame = CGRectMake(0, 0, 40, 40);
    [toggleButton setImage:[UIImage imageNamed:@"eye-closed"] forState:UIControlStateNormal];
    [toggleButton setImage:[UIImage imageNamed:@"eye-open"] forState:UIControlStateSelected];
    [toggleButton addTarget:self action:@selector(togglePasswordVisibility:) forControlEvents:UIControlEventTouchUpInside];
    passWordTF.rightView = toggleButton;
    passWordTF.rightViewMode = UITextFieldViewModeAlways;
    
    /**   下划线  */
    UIView *line1View = [[UIView alloc] init];
    line1View.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line1View];
    _mLine1View = line1View;
    
    /**    登录方式改变  */
    UIButton *loginChangeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginChangeButton setTitle:@"用密码登录" forState:UIControlStateNormal];
    [loginChangeButton setTitle:@"用密码登录" forState:UIControlStateSelected];
    [loginChangeButton setTitleColor:RGBColor(106, 168, 255) forState:UIControlStateNormal];
    [loginChangeButton setTitleColor:RGBColor(106, 168, 255) forState:UIControlStateSelected];
    loginChangeButton.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    loginChangeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    loginChangeButton.tag = 101;
    [loginChangeButton addTarget:self action:@selector(buttonChick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginChangeButton];
    _mLoginChangeButton = loginChangeButton;
    
    //登录
    UIButton *loginButton =[[UIButton alloc]init];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton setTitle:@"登录" forState:UIControlStateHighlighted];
    [loginButton setTitle:@"登录" forState:UIControlStateSelected];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:16*WidthScale>20?20:16*WidthScale];
    loginButton.backgroundColor = RGBColor(106, 168, 255);
    loginButton.tag =200;
    [loginButton addTarget:self action:@selector(buttonChick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginButton];
    _loginButton = loginButton;
    
    /**   去登录  */
    UIView *loginView = [[UIView alloc] init];
    [self addSubview:loginView];
    _mloginView = loginView;
    
    UILabel *goTitleLaber = [[UILabel alloc]init];
    goTitleLaber.text = @"没有账号？";
    goTitleLaber.font = [UIFont systemFontOfSize:fontSize];
    goTitleLaber.textColor = [UIColor blackColor];
    [loginView addSubview:goTitleLaber];
    _mGoTitleLaber = goTitleLaber;
    
    UIButton *goLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [goLoginButton setTitle:@"去注册" forState:UIControlStateNormal];
    [goLoginButton setTitle:@"去注册" forState:UIControlStateSelected];
    [goLoginButton setTitleColor:RGBColor(106, 168, 255) forState:UIControlStateNormal];
    [goLoginButton setTitleColor:RGBColor(106, 168, 255) forState:UIControlStateSelected];
    goLoginButton.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    goLoginButton.tag = 300;
    [goLoginButton addTarget:self action:@selector(buttonChick:) forControlEvents:UIControlEventTouchUpInside];
    [loginView addSubview:goLoginButton];
    _mGoLoginButton = goLoginButton;
    
    [self setUpLayout];
   
}
-(void)setUpLayout
{
    _mTitleLaber
    .sd_layout
    .topSpaceToView(self, 200)
    .rightSpaceToView(self, MarginSize)
    .leftSpaceToView(self, MarginSize)
    .heightIs(50);
    
    _mTitle1Laber
    .sd_layout
    .topSpaceToView(self.mTitleLaber, 0)
    .rightSpaceToView(self, MarginSize)
    .leftSpaceToView(self, MarginSize)
    .heightIs(50);
    
    /**     手机号  */
    _mNumLaber
    .sd_layout
    .topSpaceToView(self.mTitle1Laber, 50)
    .leftSpaceToView(self,MarginSize)
    .heightIs(50)
    .widthIs(50);
    
    _iphoneTF
    .sd_layout
    .topSpaceToView(self.mTitle1Laber, 50)
    .leftSpaceToView(self.mNumLaber,5)
    .rightSpaceToView(self, MarginSize)
    .heightIs(50);
    
    _mLineView
    .sd_layout
    .topSpaceToView(self.mNumLaber, 2)
    .rightSpaceToView(self, MarginSize)
    .leftSpaceToView(self, MarginSize)
    .heightIs(1);
    
    /**   验证码  */
    _mVerifyButton
    .sd_layout
    .topSpaceToView(self.mLineView, 30)
    .rightSpaceToView(self, MarginSize)
    .heightIs(50)
    .widthIs(100);
    
    _mVerifyTF
    .sd_layout
    .topSpaceToView(self.mLineView, 30)
    .rightSpaceToView(self.mVerifyButton, 5)
    .leftSpaceToView(self, MarginSize)
    .heightIs(50);
    
    /**   密码  */
    _mPassWordTF
    .sd_layout
    .topSpaceToView(self.mLineView, 30)
    .rightSpaceToView(self, MarginSize)
    .leftSpaceToView(self, MarginSize)
    .heightIs(50);
    
    _mLine1View
    .sd_layout
    .topSpaceToView(self.mVerifyTF, 2)
    .rightSpaceToView(self, MarginSize)
    .leftSpaceToView(self, MarginSize)
    .heightIs(1);
    
    /**   登录方式改变  */
    _mLoginChangeButton
    .sd_layout
    .topSpaceToView(self.mLine1View, 5)
    .leftSpaceToView(self, MarginSize)
    .heightIs(30)
    .widthIs(150);
    
    /**    登录  */
    _loginButton.sd_layout
    .topSpaceToView(self.mLoginChangeButton, 50)
    .centerXEqualToView(self)
    .rightSpaceToView(self, MarginSize)
    .leftSpaceToView(self, MarginSize)
    .heightIs(50);
    
    _loginButton.layer.cornerRadius = 20;
    
    /**    去登录  */
    _mloginView
    .sd_layout
    .bottomSpaceToView(self, MarginSize)
    .centerXEqualToView(self)
    .heightIs(30)
    .widthIs(140);
    
    _mGoTitleLaber
    .sd_layout
    .heightIs(30)
    .widthIs(80);
    
    _mGoLoginButton
    .sd_layout
    .leftSpaceToView(self.mGoTitleLaber, 5)
    .heightIs(30)
    .widthIs(55);
    
}

// 切换密码可见性方法
- (void)togglePasswordVisibility:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.mPassWordTF.secureTextEntry = !sender.selected;
}

// 实现代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder]; // 点击完成键收起键盘
    return YES;
}

-(void)getidentifierForVendor{
    
    NSString* identifierNumber = [[UIDevice currentDevice].identifierForVendor UUIDString] ;
    DebugLog(@"手机序列号: %@",identifierNumber);
}
-(void)getVersionCode
{
    //获取手机的型号
    NSString* phoneModel = [CommonTools iphoneType];
    DebugLog(@"phoneModel = %@",phoneModel);
}

-(void)buttonChick:(UIButton*)button
{
    if (button.tag==101)
    {
        if([self.mLoginChangeButton.titleLabel.text isEqualToString:@"用密码登录"])
        {
            [self.mLoginChangeButton setTitle:@"用验证码登录" forState:UIControlStateNormal];
            [self.mLoginChangeButton setTitle:@"用验证码登录" forState:UIControlStateSelected];
            self.mVerifyTF.hidden = YES;
            self.mVerifyButton.hidden = YES;
            self.mPassWordTF.hidden = NO;
        }else
        {
            [self.mLoginChangeButton setTitle:@"用密码登录" forState:UIControlStateNormal];
            [self.mLoginChangeButton setTitle:@"用密码登录" forState:UIControlStateSelected];
            self.mVerifyTF.hidden = NO;
            self.mVerifyButton.hidden = NO;
            self.mPassWordTF.hidden = YES;
        }
    }else if (button.tag==100)
    {
        button.selected = NO;
        [self.mVerifyTF becomeFirstResponder];
        [self startTime];
    }else if (button.tag==300)
    {
        if (self.goRegisterBtnBlock) {
            self.goRegisterBtnBlock();
        }
    }
}

#pragma mark 倒计时时间
-(void)startTime{
    count=59;
    __weak __typeof(self) weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    self._timer  =_timer;
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        
        if(count<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [weakSelf.mVerifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                [weakSelf.mVerifyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                weakSelf.mVerifyButton.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = count % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [weakSelf.mVerifyButton setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                [weakSelf.mVerifyButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
                weakSelf.mVerifyButton.userInteractionEnabled = NO;
            });
            count--;
        }
    });
    dispatch_resume(_timer);
}

@end
    
